import { createAction } from 'redux-actions'
import {
  INIT_MAIN_PAGE_C_TYPE,
  UPDATE_MAIN_PAGE_C_TYPE,
  UPDATE_MY_CENTER_NUMBERS_TYPE,
  UPDATE_USER_ARCHIVE_EDUCATION_TYPE,
  UPDATE_USER_ARCHIVE_IMG_TYPE,
  UPDATE_USER_ARCHIVE_WORK_TYPE
} from './type'
import {
  getUserArchiveBundle,
  delEducation,
  findProfession,
  findSchool,
  getUserArchiveEducation,
  insertEducation,
  updateEducation,
  delWork,
  getUserArchiveWork,
  insertWork,
  updateWork,
  getArchiveInfo,
  myCenterNumbers
} from 'src/utils/RequestAPI'
/* sync */
export const INIT_MAIN_PAGE_C_ACTION = createAction(
  INIT_MAIN_PAGE_C_TYPE,
  payload => payload
)
/**
 * 更新用户信息
 */
export const UPDATE_MAIN_PAGE_C_ACTION = createAction(
  UPDATE_MAIN_PAGE_C_TYPE,
  payload => payload
)

/* async */
export const DO_REQUEST_MAIN_PAGE_C_ACTION = createAction(
  UPDATE_MAIN_PAGE_C_TYPE,
  (...args) => {
    return new Promise((resolve, reject) => {
      let archiveInfo
      getArchiveInfo(...args).then((res) => {
        const { archiveComplete = 0 } = res.data
        archiveInfo = {
          ...res.data,
          archiveComplete: parseInt((archiveComplete * 100).toFixed(0))
        }
        return getUserArchiveBundle(...args)
      }).then(res => {
        /* 处理求职意愿数据 */
        let { jobHuntingModels } = res.data.archive
        jobHuntingModels = jobHuntingModels.map(e => ({
          ...e,
          ...JSON.parse(e.comprehensive)
        }))
        resolve({
          ...res.data.archive,
          archiveInfo,
          jobHuntingModels,
          educations: res.data.education
        })
      })
        .catch(() => {
          reject()
        })
    })
  }
)
/* 请求个人中心数据量 */
export const DO_REQUEST_MY_CENTER_NUMBERS_ACTION = createAction(
  UPDATE_MY_CENTER_NUMBERS_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      myCenterNumbers({ params: args, headers: { archiveId: args.archiveId || '' } }).then(res => {
        if (res.code === 200) {
          resolve({ myCenterNumbers: res.data })
        } else {
          reject(res)
        }
      })
    })
  }
)
/* 更新个人中心数据量 */
export const UPDATE_MY_CENTER_NUMBERS_ACTION = createAction(
  UPDATE_MY_CENTER_NUMBERS_TYPE,
  payload => payload
)
/**
 * 更新教育信息
 */
export const UPDATE_USER_ARCHIVE_EDUCATION_ACTION = createAction(
  UPDATE_USER_ARCHIVE_EDUCATION_TYPE,
  payload => payload
)

/* async */
export const DO_REQUEST_USER_ARCHIVE_EDUCATION = createAction(
  UPDATE_USER_ARCHIVE_EDUCATION_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      getUserArchiveEducation({ params: args }).then(res => {
        if (res.code === 200) {
          resolve({ educations: res.data })
        } else {
          reject(res)
        }
      })
    })
  }
)

/**
 * 添加教育信息
 */
export const DO_REQUEST_INSERT_EDUCATION = createAction(
  UPDATE_USER_ARCHIVE_EDUCATION_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      insertEducation({ params: args }).then(res => {
        if (res.code === 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      })
    })
  }
)

/**
 * 修改教育信息
 */
export const DO_REQUEST_UPDATE_EDUCATION = createAction(
  UPDATE_USER_ARCHIVE_EDUCATION_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      updateEducation({ params: args }).then(res => {
        if (res.code === 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      })
    })
  }
)

/**
 *删除教育信息
 */
export const DO_REQUEST_DEL_EDUCATION = createAction(
  UPDATE_USER_ARCHIVE_EDUCATION_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      delEducation({ params: args }).then(res => {
        if (res.code === 200) {
          resolve({ educations: res.data })
        } else {
          reject(res)
        }
      })
    })
  }
)
/**
 *查询专业信息
 */
export const DO_REQUEST_FIND_PROFESSION = createAction(
  UPDATE_USER_ARCHIVE_EDUCATION_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      findProfession({ params: args }).then(res => {
        if (res.code === 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      })
    })
  }
)
/**
 *查询专学校信息
 */
export const DO_REQUEST_FIND_SCHOOL = createAction(
  UPDATE_USER_ARCHIVE_EDUCATION_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      findSchool({ params: args }).then(res => {
        if (res.code === 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      })
    })
  }
)

/**
 * 更新工作经验
 */
export const UPDATE_USER_ARCHIVE_WORK_ACTION = createAction(
  UPDATE_USER_ARCHIVE_WORK_TYPE,
  payload => payload
)

/**
 * 获取工作经验
 */
export const DO_REQUEST_USER_ARCHIVE_WORK = createAction(
  UPDATE_USER_ARCHIVE_WORK_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      getUserArchiveWork({ params: args }).then(res => {
        if (res.code === 200) {
          resolve({ userArchiveWorkModels: res.data })
        } else {
          reject(res)
        }
      })
    })
  }
)

/**
 * 添加工作经验
 */
export const DO_REQUEST_INSERT_WORK = createAction(
  UPDATE_USER_ARCHIVE_WORK_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      insertWork({ params: args }).then(res => {
        if (res.code === 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      })
    })
  }
)

/**
 * 修改工作经验
 */
export const DO_REQUEST_UPDATE_WORK = createAction(
  UPDATE_USER_ARCHIVE_WORK_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      updateWork({ params: args }).then(res => {
        if (res.code === 200) {
          resolve(res.data)
        } else {
          reject(res)
        }
      })
    })
  }
)

/**
 *删除工作经验
 */
export const DO_REQUEST_DEL_WORK = createAction(
  UPDATE_USER_ARCHIVE_WORK_TYPE,
  args => {
    return new Promise((resolve, reject) => {
      delWork({ params: args }).then(res => {
        if (res.code === 200) {
          resolve({ userArchiveWorkModels: res.data })
        } else {
          reject(res)
        }
      })
    })
  }
)

/**
 * 更新教育信息
 */
export const UPDATE_USER_ARCHIVE_IMG_ACTION = createAction(
  UPDATE_USER_ARCHIVE_IMG_TYPE,
  payload => payload
)
